package com.naughty.userlogin02.controller;
import com.alibaba.fastjson.JSON;
import com.naughty.userlogin02.bean.QueryInfo;
import com.naughty.userlogin02.bean.Student;
import com.naughty.userlogin02.dao.StudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/*
 **@Author:刘淑钰
 **Created on 2021/12/20
 */
@RestController
public class StudentController {
    @Autowired
    StudentDao studentDao;

    @CrossOrigin
    @RequestMapping("/allStudent")
    public String getStudentList(QueryInfo queryInfo){

        System.out.println("hhhhhhhhhhhh");
        System.out.println(queryInfo);
        int numbers = studentDao.getStudentCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数
        System.out.println("aaaaaa");
        System.out.println(numbers);

        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Student> users = studentDao.getAllStudent("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",users);
        System.out.println("总条数："+numbers);
        String users_json = JSON.toJSONString(res);
        System.out.println("pppppp");
        System.out.println(users_json);
        return users_json;
    }

    @RequestMapping("/addStudent")
    public String addStudent(@RequestBody Student student){
        System.out.println("bbbbbbbbbbbb");
        System.out.println(student);
//        student.setRole("普通用户");
//        student.setSpwd("12345");
        int i = studentDao.addStudent(student);
        String str = i >0?"success":"error";
        System.out.println("bbaaaa");
        System.out.println(i);
        return str;//也可以通过这里返回报错
    }

    @RequestMapping("/editStudent")
    public String editStudent(@RequestBody Student student){

        System.out.println("bbaaaa");
        System.out.println(student);
        System.out.println("kkkkkk");
        int i = studentDao.editStudent(student);
        System.out.println("lllllll");
        String str = i >0?"success":"error";
        System.out.println(str);
        return str;
    }

    @RequestMapping("/delete1")
    public String deleteStudent(@RequestBody Student student){
        System.out.println("俺在删除了别着急");
        System.out.println(student.getSid());
        int i = studentDao.deleteStudent(student.getSid());
        System.out.println("哈哈哈啊哈");
        System.out.println(i);
        System.out.println("噗噗噗噗噗");
        String str = i >0?"success":"error";
        return str;
    }
}
